package com.df.base.updater;



import com.df.base.other.SpringContextHelper;
import com.df.base.updater.annotation.EnableGroovyUpdate;
import com.df.base.updater.impl.DBGroovyUpdater;
import com.df.base.updater.impl.DevGroovyUpdater;
import com.df.base.updater.impl.FileGroovyUpdater;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.boot.env.OriginTrackedMapPropertySource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.io.support.SpringFactoriesLoader;
import org.springframework.core.type.AnnotationMetadata;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class GroovyUpdaterRegister implements  ImportBeanDefinitionRegistrar{

    public final String DEV = "dev";
    public final String PRODUCT = "product";



    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        Map<String, Object> annotationAttributes = importingClassMetadata.getAnnotationAttributes(EnableGroovyUpdate.class.getName());
        if(annotationAttributes.isEmpty()){
            log.error("EnableGroovyUpdate注解所在的bean实例不存在，添加updater失败");
            return;
        }
        long delay = (long) annotationAttributes.get("delay");
        long interval = (long) annotationAttributes.get("interval");
        Object localScriptPath = annotationAttributes.get("localScriptPath");
        String cachePath = (String) annotationAttributes.get("cachePath");
        interval=interval<60000?60000:interval;

        GroovyUpdateContext.delay = delay;
        GroovyUpdateContext.interval = interval;
        if(localScriptPath!=null){
            GroovyUpdateContext.localScriptPath.addAll(Arrays.asList((String[]) localScriptPath)) ;
        }

        GroovyUpdateContext.cachePath = cachePath;
        List<String> classNames = SpringFactoriesLoader.loadFactoryNames(IGroovyUpdater.class, null);

        if(!registry.containsBeanDefinition(SpringContextHelper.class.getName())){
            registry.registerBeanDefinition(SpringContextHelper.class.getName(),BeanDefinitionBuilder.genericBeanDefinition(SpringContextHelper.class).getBeanDefinition());
        }

        for (String className : classNames) {
            BeanDefinitionBuilder builder =  BeanDefinitionBuilder.genericBeanDefinition(className);
            builder.setScope(BeanDefinition.SCOPE_SINGLETON);
            registry.registerBeanDefinition(className, builder.getBeanDefinition());
        }
    }


}
